package cn.pw.test.com.news.fragment;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import cn.pw.test.com.news.R;
import cn.pw.test.com.news.adapter.CalendarAdapter;
import cn.pw.test.com.news.bean.CalendarBean;
import cn.pw.test.com.news.server.NewsBack;
import cn.pw.test.com.news.server.NewsGlobal;
import tech.com.commoncore.base.BaseFragment;
/**
 * @deprecated
 */
public class CalendarFragment extends BaseFragment {

    private SmartRefreshLayout smartRefresh;
    private RecyclerView calendarRecycler;

    private CalendarView calendarView;
    private TextView moneyText;

    private CalendarAdapter calendarAdapter;

    private ArrayList<CalendarBean> calendarBeans = new ArrayList<>();

    private Calendar mCalendar;

    public static CalendarFragment newInstance() {
        CalendarFragment fragment = new CalendarFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_calendar;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        calendarRecycler = mContentView.findViewById(R.id.news_calendar_recycler_view);
        smartRefresh = mContentView.findViewById(R.id.news_calendar_smart_refresh);
        calendarView = mContentView.findViewById(R.id.calendar_news_calendar_view);
        moneyText = mContentView.findViewById(R.id.calendar_news_money_text);

        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
//        dividerItemDecoration.setDrawable(mContext.getResources().getDrawable(R.drawable.divider_recycler_vertical));
        calendarRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        calendarRecycler.addItemDecoration(dividerItemDecoration);
        calendarAdapter = new CalendarAdapter(R.layout.news_layout_item_calendar);
        calendarRecycler.setAdapter(calendarAdapter);

        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                if (mCalendar != null)
                    requestCalendar(mCalendar.getYear(), mCalendar.getMonth(), mCalendar.getDay());
            }
        });

        calendarView.setOnMonthChangeListener(new CalendarView.OnMonthChangeListener() {
            @Override
            public void onMonthChange(int year, int month) {
                moneyText.setText(String.valueOf(month) + "月");
            }
        });

        calendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(Calendar calendar) {
            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                mCalendar = calendar;
                smartRefresh.autoRefresh();
            }
        });
    }

    private void requestCalendar(int year, int money, int day) {
        NewsGlobal.getNewsImp().requestCalendarNews(year, money, day, 0, "", new NewsBack<List<CalendarBean>>() {
            @Override
            public void onBack(List<CalendarBean> beans, String err) {
                smartRefresh.finishRefresh();
                if (beans != null) {
                    calendarBeans.clear();
                    calendarBeans.addAll(beans);
                    calendarAdapter.replaceData(calendarBeans);
                    calendarAdapter.notifyDataSetChanged();
                }
            }
        });
    }
}
